<?php
/**
 * Created by PhpStorm.
 * User: 张辉
 * Date: 2018/8/6
 * Time: 12:58
 */
namespace app\common\redis;

/*
 * 用户计数redis
 */
use app\common\model\community\CommunityCommentModel;

class UserCountingRedis extends RedisModel
{
    /**
     * Redis配置名称
     * @var string
     */
    public $redis = 'default';

    // 用户点赞数key
    public $user_up_count_redis_key = "zset:user_up_count";

    // 用户粉丝总数key
    public $user_fans_count_redis_key = "zset:user_fans_count";

    // 用户关注总数key
    public $user_follow_count_redis_key = "zset:user_follow_count";

    // 增加一个用户的点赞
    public function add_user_up( $user_id , $number = 1 )
    {
        return $this->ZIncrBy( $this->user_up_count_redis_key, $number, $user_id );
    }

    /**
     * 减少一个用户的点赞
     */
    public function delete_user_up( $user_id , $number = -1 )
    {
        return $this->ZIncrBy( $this->user_up_count_redis_key, $number, $user_id );
    }

    /**
     * 返回一个用户的获赞总数
     * @param $user_id
     * @return int
     */
    public function get_user_up_count( $user_id )
    {
        return (int) $this->ZScore( $this->user_up_count_redis_key, $user_id );
    }

    // 增加一个用户粉丝
    public function add_user_fans( $user_id )
    {
        return $this->ZIncrBy( $this->user_fans_count_redis_key, 1, $user_id );
    }

    /**
     * 减少一个用户的粉丝
     */
    public function delete_user_fans( $user_id )
    {
        return $this->ZIncrBy( $this->user_fans_count_redis_key, -1, $user_id );
    }

    /**
     * 返回一个用户的粉丝总数
     * @param $user_id
     * @return int
     */
    public function get_user_fans_count( $user_id )
    {
        return (int) $this->ZScore( $this->user_fans_count_redis_key, $user_id );
    }

    // 增加一个用户的关注
    public function add_user_follow( $user_id )
    {
        return $this->ZIncrBy( $this->user_follow_count_redis_key, 1, $user_id );
    }

    /**
     * 减少一个用户的关注
     */
    public function delete_user_follow( $user_id )
    {
        return $this->ZIncrBy( $this->user_follow_count_redis_key, -1, $user_id );
    }

    /**
     * 返回一个用户的关注总数
     * @param $user_id
     * @return int
     */
    public function get_user_follow_count( $user_id )
    {
        return (int) $this->ZScore( $this->user_follow_count_redis_key, $user_id );
    }

    /**
     * 同步点赞数据
     */
    public function up_format()
    {
        $CommunityCommentModel = CommunityCommentModel::getInstance();
        $user_count_list = $CommunityCommentModel->query('SELECT
            community_comment.user_id,
            COUNT( * ) AS count 
        FROM
            community_comment_rate,
            community_comment 
        WHERE
            community_comment.id = community_comment_rate.comment_id 
        
        GROUP BY
            community_comment.user_id'
        );

        dump( $user_count_list );

        $user_count_list_format = [];
        foreach ( $user_count_list as $value ){
            $user_count_list_format[$value['user_id']] = $value['count'];
        }

        dump( $user_count_list_format );

        $this->Delete( $this->user_up_count_redis_key );

        $this->SortSetZmadd( $user_count_list_format ,$this->user_up_count_redis_key );
    }



}